package com.yuwei.recyclerviewdemo;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.AttributeSet;

/**
 * @author 崔健伟
 * @version V1.0
 * @Title: MySwipeRefreshLayout.java
 * @Package com.yuwei.recyclerviewdemo
 * @Description: 自定义SwipeRefreshLayout，实现RecyclerView不需要二次定义
 * @date 2016/11/20 16:59
 */
public class MySwipeRefreshLayout extends SwipeRefreshLayout {
    public static final String TAG = "MySwipeRefreshLayout";
    //用于替换ListView
    private RecyclerView recyclerView;
    private Context mContext;
    private MyScollListener myScollListener;
    private GridLayoutManager mLayoutManager;
    //标示是否正在进行上拉加载
    private boolean isLoading;

    public MySwipeRefreshLayout(Context context) {
        super(context);
        this.mContext = context;
    }

    public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        this.mContext = context;
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:02
     * @desc: 初始化自定义的MyScollListener
     **/
    public void setMyScollListener(MyScollListener myScollListener) {
        this.myScollListener = myScollListener;
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 14:58
     * @desc: 初始化自定义控件中的内容，添加RecyclerView到SwipeRefreshLayout中
     **/
    private void initView(Context context) {
        recyclerView = new RecyclerView(context);
        setGridLayoutManager(1);
        addView(recyclerView);
        recyclerView.addOnScrollListener(myScrollListener);
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:04
     * @desc: 提供对外的方法，用于设置设置GridLayoutManager的显示列数
     **/
    public void setGridLayoutManager(int i) {
        initDate(i);
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:03
     * @desc: 设置GridLayoutManager的显示列数，如果不设置则默认为1，即默认为listView
     **/
    private void initDate(int i) {
        mLayoutManager = new GridLayoutManager(mContext, i);
        recyclerView.setLayoutManager(mLayoutManager);
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:05
     * @desc: 提供对外的方法，设置RecyclerView的adapter
     **/
    public void setAdapter(RecyclerView.Adapter adapter) {
        setRecyclerViewAdapter(adapter);
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:06
     * @desc: 设置RecyclerView的adapter
     **/
    private void setRecyclerViewAdapter(RecyclerView.Adapter adapter) {
        recyclerView.setAdapter(adapter);
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:06
     * @desc: RecyclerView的滑动监听，用于实现上拉加载
     **/
    private OnScrollListener myScrollListener = new OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int last = mLayoutManager.findLastCompletelyVisibleItemPosition();
            if (recyclerView.getAdapter().getItemCount() - 1 == last && !isLoading && !getIsRefreshing()) {
                if (myScollListener != null) {
                    myScollListener.myScroll();
                }
            }
        }
    };

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:06
     * @desc: 提供对外的接口，实现isLoading的赋值
     **/
    public void setLoading(boolean loading) {
        isLoading = loading;
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:08
     * @desc: 提供对外的方法，用于设置SwipeRefreshLayout是否能够下拉刷新
     **/
    public void setRefreshEnabled(boolean enabled) {
        setEnabled(enabled);
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:09
     * @desc: 提供对外的方法，用于设置下拉刷新进度条的颜色，最多可设置六个
     **/
    public void setRefreshColors(@ColorInt int... colors) {
        setColorSchemeColors(colors);
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:09
     * @desc: 提供对外的方法，用于设置滚动条渐变的时间
     **/
    public void setScrollBareFadeDuration(int duration) {
        setScrollBarFadeDuration(duration);
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:10
     * @desc: 提供对外的方法，用于设置是否取消下来刷新
     **/
    public void setIsRefreshing(boolean refreshing) {
        setRefreshing(refreshing);
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:11
     * @desc: 提供对外的方法，用于获取当前是否正在进行下拉刷新
     **/
    public boolean getIsRefreshing() {
        return isRefreshing();
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:11
     * @desc: 提供对外的方法，用于设置SwipeRefreshLayout的下拉刷新监听
     **/
    public void setInRefreshListener(OnRefreshListener listener) {
        setOnRefreshListener(listener);
    }

    /**
     * @author: cuijianwei
     * @time: 2016/11/21 15:12
     * @desc: 定义对外的接口，用于监听上拉加载的事件
     **/
    public interface MyScollListener {
        void myScroll();
    }
}
